package generator;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.dom4j.*;

import junit.framework.TestCase;

public class Dom4jOpenOfficeDocTest extends TestCase {

    public static void main(String[] args) {
        junit.swingui.TestRunner.run(OpenOfficeDocTest.class);
    }

    public void testGetFontDecl() {
    	try {
    		OpenOfficeDoc doc = OpenOfficeDocManager.read("testing/generator/simple301.odt");
    		Dom4jOpenOfficeDoc dom4jDoc = new Dom4jOpenOfficeDoc(doc);
    		System.out.println(dom4jDoc.getFontDeclrs().asXML());
    		
    		dom4jDoc.listStyles();
		} catch (IOException e) {
			fail("couldn't find file");
		} catch (DocumentException e) {
			fail("couldn't convert doc to xml");
		}
    }

    public void testGetBody() {
    	try {
    		OpenOfficeDoc doc = OpenOfficeDocManager.read("testing/generator/simple301.odt");
    		Dom4jOpenOfficeDoc dom4jDoc = new Dom4jOpenOfficeDoc(doc);
    		Node node = dom4jDoc.getBody();
    		System.out.println(node.asXML());
		} catch (IOException e) {
			fail("couldn't find file");
		} catch (DocumentException e) {
			fail("couldn't convert doc to xml");
		}
    }

    public void testGetText() {
    	try {
    		OpenOfficeDoc doc = OpenOfficeDocManager.read("testing/generator/simple301.odt");
    		Dom4jOpenOfficeDoc dom4jDoc = new Dom4jOpenOfficeDoc(doc);
    		Node text = dom4jDoc.getText();
    		Iterator<Node> it = text.selectNodes("//text:p").iterator();
    		int line = 1;
    		while (it.hasNext()) {
    			Node p = it.next();
    			System.out.println(line+": "+p.getStringValue());
    			line++;
    		}
		} catch (IOException e) {
			fail("couldn't find file");
		} catch (DocumentException e) {
			fail("couldn't convert doc to xml");
		}
    }

    public void testHeadings() {
    	try {
    		OpenOfficeDoc doc = OpenOfficeDocManager.read("testing/generator/simple301.odt");
    		Dom4jOpenOfficeDoc dom4jDoc = new Dom4jOpenOfficeDoc(doc);
    		List nodes = dom4jDoc.getHeadings();
    		Iterator it = nodes.iterator();
    		while (it.hasNext()) {
    			Node node = (Node)it.next();
    			System.out.print("<"+node.valueOf("@text:style-name")+">");
    			System.out.print(node.getStringValue());
    			System.out.print("</"+node.valueOf("@text:style-name")+">\n");
    		}
		} catch (IOException e) {
			fail("couldn't find file");
		} catch (DocumentException e) {
			fail("couldn't convert doc to xml");
		}
    }

    public void testHeadedBlock() {
    	try {
    		OpenOfficeDoc doc = OpenOfficeDocManager.read("testing/generator/simple301.odt");
    		Dom4jOpenOfficeDoc dom4jDoc = new Dom4jOpenOfficeDoc(doc);

    		List nodes = dom4jDoc.getHeadedBlock();
    		Iterator it = nodes.iterator();
    		while (it.hasNext()) {
    			Node node = (Node)it.next();
    			System.out.print("<"+node.valueOf("@text:style-name")+">");
    			System.out.print(node.getStringValue());
    			System.out.print("</"+node.valueOf("@text:style-name")+">\n");
    			System.out.println(node.getUniquePath());
    		}
		} catch (IOException e) {
			fail("couldn't find file");
		} catch (DocumentException e) {
			fail("couldn't convert doc to xml");
		}
    }

}
